class ProjectsController < ApplicationController
layout "m"

  before_filter :login_required
  before_filter :group_setup
  
  def group_setup
    if params[:group_id]
      @group_id = params[:group_id]
    elsif current_user.groups.size == 1
      @group_id = current_user.groups[0].id
    else
      @group_id =1
      #flash[:notice] = "Your group was not selected or does not exist"
      #redirect_to :controller => 'groups', :action => :index
    end
  end

  def edit
    @action_item = ActionItem.find(params[:id])
    @action_item_entries = ActionItemEntry.find(:all, :conditions =>["action_item_id = ?", params[:id]], :order => "created_at ASC")
    @projects = Project.find(:all,:conditions=>["status!=0"])
    @users = User.find(:all)
    return unless request.post?
    entry = ActionItemEntry.new(params[:action_item_entry])
    entry.created_by = current_user
    @action_item.action_item_entries << entry
    if @action_item.first_entry.status == "Closed"
      @action_item.status = 0
    else
      @action_item.status = 1
    end
    @action_item.shead = entry.name
    @action_item.sbody = entry.note
    @action_item.smeta = entry.note
    if @action_item.save
      redirect_to :action => 'edit'
    else
        flash[:error] = entry.errors.full_messages
    end
  end
  
  def edit_project
    @project = Project.find(params[:id])
    @project_entries = ProjectEntry.find(:all, :conditions =>["project_id = ?", params[:id]], :order => "created_at ASC")
    @action_items = ActionItem.find(:all, :conditions =>["project_id =?", params[:id]])
    return unless request.post?
    entry = ProjectEntry.new(params[:project_entry])
    entry.created_by = current_user
    @project.project_entries << entry
    if @project.first_entry.status == "Closed"
      for item in @action_items
        item.status = 0
        item.save
      end
      @project.status = 0
    else
      @project.status = 1
    end
    @project.shead = entry.name
    @project.sbody = entry.note
    @project.smeta = entry.note
    if @project.save
      redirect_to :action => 'edit_project'
    else
        flash[:error] = entry.errors.full_messages
    end
  end
  
  def index
  end
  def lists
    if params[:viewoptions]
      current_user.pref2 = params[:viewoptions][:view]
      current_user.save!
    end
    @projects = Project.find(:all,:conditions => ["status != 0"])
    @actions = ActionItem.find(:all,:conditions => ["status != 0"]) 
    render :partial => 'lists', :locals => { :@projects => @projects, :@actions => @actions }
  end
  
  def new
    if Project.find(:all,:conditions=>["status!=0"]).length() > 0
    else
      redirect_to :action => :index
      flash[:error] = "You must first create a project before adding action items"
    end
    @projects = Project.find(:all,:conditions=>["status!=0"])
    @users = User.find(:all)
    # save action item and entry
    @action_item = ActionItem.new(params[:action_item])    
    return unless request.post? 
    entry = ActionItemEntry.new(params[:action_item_entry])
    entry.created_by = current_user
    @action_item.status = 1
    @action_item.shead = entry.name
    @action_item.sbody = entry.note
    @action_item.smeta = entry.note
    @action_item.action_item_entries << entry
    if @action_item.save
      redirect_to :action => 'index'
    else
      flash[:error] = entry.errors.full_messages
    end
  end
  
  def new_project
    # save project and entry
    @project = Project.new(params[:project])
    return unless request.post?
    entry = ProjectEntry.new(params[:project_entry])
    entry.created_by_id = current_user.id
    @project.status = 1
    @project.shead = entry.name
    @project.sbody = entry.note
    @project.smeta = entry.note
    @project.project_entries << entry
    if @project.save
      redirect_to :action => 'index'
    else
        flash[:error] = entry.errors.full_messages
    end
  end
end
